package com.koro.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.koro.DTO.FindJobInfoLogDTO;
import com.koro.entity.JobInfoLog;
import com.koro.mapper.JobInfoLogMapper;
import com.koro.service.JobInfoLogService;
import com.koro.text.Convert;
import com.koro.tool.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JobInfoLogServiceImpl implements JobInfoLogService {
    @Autowired
    private JobInfoLogMapper jobInfoLogMapper;

    /**
     * 获取quartz调度器日志的计划任务
     */
    @Override
    public List<JobInfoLog> queryList(FindJobInfoLogDTO dto) {
        return jobInfoLogMapper.selectList(Wrappers.<JobInfoLog>lambdaQuery()
                .eq(StringUtils.isNotEmpty(dto.getJobName()), JobInfoLog::getJobName, dto.getJobName())
                .eq(StringUtils.isNotEmpty(dto.getJobGroup()), JobInfoLog::getJobGroup, dto.getJobGroup())
                .eq(StringUtils.isNotEmpty(dto.getStatus()), JobInfoLog::getStatus, dto.getStatus())
                .ge(StringUtils.isNotBlank(dto.getStartTime()), JobInfoLog::getStartTime, dto.getStartTime())
                .le(StringUtils.isNotBlank(dto.getEndTime()), JobInfoLog::getEndTime, dto.getEndTime() + " 23:59:59 ")
                .orderByDesc(JobInfoLog::getJobLogId)
            //    .last(SqlUtil.getLimitSql(dto.getCurrentPage(), dto.getShowCount()))
        );
    }

    @Override
    public Long queryTotal(FindJobInfoLogDTO dto) {
        return jobInfoLogMapper.selectCount(Wrappers.<JobInfoLog>lambdaQuery()
                .eq(StringUtils.isNotEmpty(dto.getJobName()), JobInfoLog::getJobName, dto.getJobName())
                .eq(StringUtils.isNotEmpty(dto.getJobGroup()), JobInfoLog::getJobGroup, dto.getJobGroup())
                .eq(StringUtils.isNotEmpty(dto.getStatus()), JobInfoLog::getStatus, dto.getStatus())
                .ge(StringUtils.isNotBlank(dto.getStartTime()), JobInfoLog::getStartTime, dto.getStartTime())
                .le(StringUtils.isNotBlank(dto.getEndTime()), JobInfoLog::getEndTime, dto.getEndTime() + " 23:59:59 ")
        );
    }

    /**
     * 通过调度任务日志ID查询调度信息
     *
     * @param jobLogId 调度任务日志ID
     * @return 调度任务日志对象信息
     */
    @Override
    public JobInfoLog findJobLogById(Long jobLogId) {
        return jobInfoLogMapper.selectById(jobLogId);
    }

    /**
     * 新增任务日志
     *
     * @param jobLog 调度日志信息
     */
    @Override
    public void saveJobLog(JobInfoLog jobLog) {
        jobInfoLogMapper.insert(jobLog);
    }

    /**
     * 批量删除调度日志信息
     *
     * @param ids 需要删除的数据ID
     */
    @Override
    public void deleteJobLogByIds(String ids) {

        String[] array = Convert.toStrArray(ids);
        for (String id : array) {
            jobInfoLogMapper.deleteById(Convert.toLong(id));
        }
    }


    /**
     * 清空任务日志
     */
    @Override
    public void deleteAllJobLog() {
        jobInfoLogMapper.delete(null);
    }
}
